package sum.book.auth.server.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import sum.book.auth.dto.user.CustomUser;

import java.util.HashMap;
import java.util.Map;

@Slf4j
//@Service
public class CustomJwtTokenEnhancerImpl implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

        Authentication userAuthentication = authentication.getUserAuthentication();//登录用户信息
        CustomUser customUser = (CustomUser) userAuthentication.getPrincipal();

        // 自定义一些扩展字段
        Map<String, Object> info = new HashMap<>();
        info.put("hello", "word");

        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);//设置附加信息

        return accessToken;
    }

}
